package com.xianbeiying.app.ckgl.widght.brokenline;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.widght.AppManager;

/**
 * 自定义折线图
 */
public class MyChartView extends View {
    //x轴
    private Paint paintX;
    //x轴数据
    private Paint paintXtv;
    //x轴文字
    private Paint paintXtitle;
    //y轴
    private Paint paintY;
    //y轴数据
    private Paint paintYtv;
    //y轴文字
    private Paint paintYtitle;
    //折线数据1
    private Paint paintData1;
    //折线数据2
    private Paint paintData2;
    //折线数据3
    private Paint paintData3;

//   private Scroller scroller;
    public MyChartView(Context context) {
        super(context);
        initView(context);
    }

    public MyChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
   //初始化
    private void initView(Context context){
//          scroller=new Scroller(context);
        //获取控件的宽度
        screenW = this.getWidth();
        screenH = this.getHeight();
        int width = AppManager.getInstance().getWindowSizeEntity().getWidth();
        XLength=width/5*3;
        initPaintX();
        initPaintY();
        initPaintData();
    }
    //初始化X轴
    private void initPaintX(){
        paintX=new Paint();
        paintX.setStyle(Paint.Style.STROKE);
        paintX.setAntiAlias(true);// 去锯齿
        paintX.setColor(Color.WHITE);// 颜色
        paintX.setStrokeWidth(2);

        paintXtv=new Paint();
        paintXtv.setStyle(Paint.Style.STROKE);
        paintXtv.setAntiAlias(true);// 去锯齿
        paintXtv.setColor(getResources().getColor(R.color.white)); // 颜色
        paintXtv.setTextSize(25);
        paintXtitle=new Paint();
        paintXtitle.setStyle(Paint.Style.STROKE);
        paintXtitle.setAntiAlias(true);// 去锯齿
        paintXtitle.setColor(getResources().getColor(R.color.white));// 颜色
        paintXtitle.setTextSize(25);
    }
    //初始化X轴
    private void initPaintY(){
        paintY=new Paint();
        paintY.setStyle(Paint.Style.STROKE);
        paintY.setAntiAlias(true);// 去锯齿
        paintY.setColor(Color.WHITE);// 颜色
        paintY.setStrokeWidth(2);
        paintYtv=new Paint();
        paintYtv.setStyle(Paint.Style.STROKE);
        paintYtv.setAntiAlias(true);// 去锯齿
        paintYtv.setColor(getResources().getColor(R.color.white));// 颜色
        paintYtv.setTextSize(25);
        paintYtitle=new Paint();
        paintYtitle.setStyle(Paint.Style.STROKE);
        paintYtitle.setAntiAlias(true);// 去锯齿
        paintYtitle.setColor(getResources().getColor(R.color.white));// 颜色
        paintYtitle.setTextSize(25);
    }
    public int XPoint =130; // 原点的X坐标
    public int YPoint = 400; // 原点的Y坐标
    public int XScale = 50; // X的刻度长度
    public int YScale = 50; // Y的刻度长度
    public int XLength; // X轴的长度
    public int YLength=350; // Y轴的长度
    private float screenW, screenH;
    private float lastX;
    private float lastY;
    public String[] XLabel; // X的刻度
    public String[] YLabel; // Y的刻度
    public int[] Data; // 数据
    public String titleX,titleY;
//    private Scroller scroller;
    private float total_Width = 0;
    //初始化折线
    private void initPaintData(){
        paintData1=new Paint();
        paintData1.setStyle(Paint.Style.STROKE);
        paintData1.setAntiAlias(true);// 去锯齿
        paintData1.setColor(getResources().getColor(R.color.app_color));// 颜色
        paintData1.setStrokeWidth(2);


        paintData2=new Paint();
        paintData2.setStyle(Paint.Style.STROKE);
        paintData2.setAntiAlias(true);// 去锯齿
        paintData2.setColor(getResources().getColor(R.color.app_color));// 颜色
        paintData2.setStrokeWidth(2);


        paintData3=new Paint();
        paintData3.setStyle(Paint.Style.STROKE);
        paintData3.setAntiAlias(true);// 去锯齿
        paintData3.setColor(getResources().getColor(R.color.app_color));// 颜色
        paintData3.setStrokeWidth(2);
    }
    public void SetInfo(String[] XLabels, String[] YLabels, int[] AllData,String titleX,String titleY) {
        XLabel = XLabels;
        YLabel = YLabels;
        Data = AllData;
        this.titleX=titleX;
        this.titleY=titleY;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setPaintX(canvas);
        setPaintY(canvas);
        setPaintXtv(canvas);
        setPaintYtv(canvas);
        setPaintXtitle(canvas);
        setPaintYtitle(canvas);
        setPaintData(canvas);
    }
    //设置X轴位置
    private void setPaintX(Canvas canvas){
        // 设置X轴
        canvas.drawLine(XPoint - 50, YPoint, XPoint + XLength, YPoint, paintX); // 轴线
        canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 12,
                YPoint - 9, paintX); // 箭头
        canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 12,
                YPoint + 9, paintX);
    }
    //设置X轴文字位置
    private void setPaintXtv(Canvas canvas){
        for (int i = 0; i * XScale < XLength; i++) {
            try {
//                canvas.drawText(XLabel[i], XPoint + i * XScale - 10,
//                        YPoint + 50, paintXtv); // 文字
                canvas.drawText(XLabel[i], XPoint + i * XScale - 50,
                        YPoint + 50, paintXtv); // 文字
            } catch (Exception e) {
            }
        }
    }
    //设置X轴标题位置
    private void setPaintXtitle(Canvas canvas){
        canvas.drawText(titleX, XPoint + XLength, YPoint + 50, paintXtitle);
    }
    //设置Y轴位置
    private void setPaintY(Canvas canvas){
        // 设置Y轴
        canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint+50, paintY); // 轴线
        canvas.drawLine(XPoint, YPoint - YLength, XPoint - 9, YPoint - YLength
                + 12, paintY); // 箭头
        canvas.drawLine(XPoint, YPoint - YLength, XPoint + 9, YPoint - YLength
                + 12, paintY);
    }
    //设置Y轴文字位置
    private void setPaintYtv(Canvas canvas){
        for (int i = 0; i * YScale < YLength; i++){
            try{
                canvas.drawText(YLabel[i], XPoint-50,
                        YPoint - i * YScale + 5, paintYtv); // 文字
            }catch (Exception e){

            }
        }
    }
    //设置Y轴标题位置
    private void setPaintYtitle(Canvas canvas){
        canvas.drawText(titleY, XPoint-100, YPoint - (YLabel.length+1) * YScale+30, paintXtitle);
    }
    //设置折线位置
    private void setPaintData(Canvas canvas){
        for (int i = 0; i * XScale < XLength; i++) {
            try {
                // 数据值
                if (i > 0 && YCoord(Data[i - 1]) != -999
                        && YCoord(Data[i]) != -999) // 保证有效数据
                    canvas.drawLine(XPoint + (i - 1) * XScale,
                            YCoord(Data[i - 1]), XPoint + i * XScale,
                            YCoord(Data[i]), paintData1);
                canvas.drawLine(XPoint + (i - 1) * XScale,
                        YCoord(Data[i - 1]), XPoint + i * XScale,
                        YCoord(Data[i]), paintData2);
                canvas.drawLine(XPoint + (i - 1) * XScale,
                        YCoord(Data[i - 1]), XPoint + i * XScale,
                        YCoord(Data[i]), paintData3);
                canvas.drawCircle(XPoint + i * XScale, YCoord(Data[i]), 2,
                        paintData1);
                canvas.drawCircle(XPoint + i * XScale, YCoord(Data[i]), 2,
                        paintData2);
                canvas.drawCircle(XPoint + i * XScale, YCoord(Data[i]), 2,
                        paintData3);
            } catch (Exception e) {
            }
        }
    }
    private int YCoord(int y0) // 计算绘制时的Y坐标，无数据时返回-999
    {
        int y;
        try{
            y = y0;
        }catch(Exception e){
            return -999; // 出错则返回-999
        }
        try{
            return YPoint - y * YScale / Integer.parseInt(YLabel[1]);
        }catch (Exception e){
        }
        return y;
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                lastX=event.getX();
//                lastY=event.getY();
//                return true;
//            case MotionEvent.ACTION_MOVE:
//                float newX=event.getX();
//                float newY=event.getY();
//                scrollBy((int) ((lastX-newX)*1), (int) ((lastY-newY)*1));
//                lastX=newX;
//                lastY=newY;
//                break;
//            case MotionEvent.ACTION_UP:
//                int scrollX=getScrollX();
//                int scrollY=getScrollY();
//                if((scrollX<0)&&(scrollX<-10||scrollY>10)){
//                    //XY方向超出左边位置
//                    scroller.startScroll(scrollX,scrollY, -scrollX, -scrollY);
//                    invalidate();
//                }
//                else if((scrollX>total_Width-screenW)&&(scrollY<-10||scrollY>10)){
//                    //XY方向超出右边位置
//                    scroller.startScroll(scrollX,scrollY, (int) (total_Width-screenW-scrollX), -scrollY);
//                    invalidate();
//                }else if(scrollX<0){
//                    //X方向超出左边的位置
//                    scroller.startScroll(scrollX,scrollY, -scrollX, 0);
//                    invalidate();
//
//                }else if(scrollX>total_Width-screenW){
//                    //X方向超出右边边的位置
//                    scroller.startScroll(scrollX,scrollY, (int) (total_Width-screenW-scrollX), 0);
//                    invalidate();
//                }else if(scrollY<-10||scrollY>10){
//                    //Y方向超出了位置
//                    scroller.startScroll(scrollX,scrollY, 0, -scrollY);
//                    invalidate();
//                }
//                break;
//            default:
//                break;
//        }
//        return super.onTouchEvent(event);
//    }

//    @Override
//    public void computeScroll() {
//        if (scroller.computeScrollOffset()) {
//            // 调用这个下面的条件是由于scroller调用了滑动从而使它激发
//            scrollTo(scroller.getCurrX(), scroller.getCurrY());
//            invalidate();
//            return;
//        }
//        super.computeScroll();
//    }

}
